package com.longrise.android.byjk.plugins.login;

import android.content.Context;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;

/**
 * Created by callxkj on 2017/3/24.
 * 获取手机短信验证码
 */

public class GetMobileVfhelper {

    private static GetMobileVfhelper mHelper;

    private GetMobileVfhelper() {

    }

    public static GetMobileVfhelper getInstance() {
        if (mHelper == null) {
            mHelper = new GetMobileVfhelper();
        }
        return mHelper;
    }

    /**
     * @param mobile    手机号码
     * @param checktype 验证类别("1":通过手机号注册;"2":通过手机号登录;"3":开通手机号快捷登录;)
     * @param callback  (0:失败 1:成功;)
     */
    public void getMobileVfCode(final Context context, String mobile, String checktype, final MobileVfCodeCallback callback) {
        EntityBean bean = new EntityBean();
        bean.set("mobile", mobile);
        bean.set("checktype", checktype);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl, "bbt_login_sMobileCheckMsgCode",
                fbean, new LoadDataManager.OnRequestCallListener() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            int resultstate =  bean.getInt("resultstate");
                            String resultdesc = bean.getString("resultdesc");
                            if (resultstate == 1) {
                                EntityBean resultBean = bean.getBean("result");
                                String resultStatus = resultBean.getString("issuccess");
                                if(callback!=null){
                                    callback.onSuccess(resultStatus);
                                    DZZWTools.showToast(context,resultdesc,0);
                                }
                            }else{
                                callback.onSuccess("0");
                                DZZWTools.showToast(context,resultdesc,0);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            callback.onSuccess("0");
                            DZZWTools.showToast(context,"验证码接口解析错误",0);
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                           callback.onFinished(key,service);
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        callback.onError(key,service,ex,isOnCallback);
                        DZZWTools.showToast(context, AppUtil.getString(R.string.networkerror), 0);
                    }
                });
    }

    interface MobileVfCodeCallback {
        void onSuccess(String resultStatus);

        void onFinished(String key, String service);

        void onError(String key, String service, Throwable ex, boolean isOnCallback);
    }

}
